/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package classes;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import rad.Java.Common.Class.DefaultList;
import rad.Java.Common.Class.IList;
import rad.Java.Common.DB.GenerateSql;

/**
 *
 * @author rogerio
 */
public class contratos extends DefaultList implements IList{

    private contrato c;
    public contratos()
    {
        c = new contrato();
        _gsql = new GenerateSql();
        _gsql.setTable("contratos");
    }

    public contrato get(int index)
    {
        return (contrato) _items.get(index);
    }

    public boolean fillList() {
        _gsql.prepare();
        _gsql.setCommandType(GenerateSql.CommandType.CMD_SELECT);
        _gsql.setValue("","*");
        try {
            return mountList();
        } catch (SQLException ex) {
            Logger.getLogger(contrato.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public boolean fillList(String sql) throws SQLException
    {
        _gsql.prepare();
        _gsql.setCommandType(GenerateSql.CommandType.CMD_SELECT);
        _gsql.setValue("","*");
        _gsql.setConditionalWhere(sql);

        return mountList();
    }

    public boolean searchByYear(String estab_id, String year) throws SQLException
    {
        _gsql.prepare();
        _gsql.setCommandType(GenerateSql.CommandType.CMD_SELECT);
        _gsql.setValue("", "*");
        _gsql.setConditionalWhere("estabelecimentos_id = "+estab_id);
        _gsql.setConditionalWhere("ano = "+year);

        if(!mountList())
           return false;

        if(_items.size() > 0)
            return true;
        return false;
    }

    private boolean mountList() throws SQLException
    {
        ResultSet rs;
        if (!_gsql.execute(false))
            return false;
        clear();

        while(_gsql.next())
        {
            contrato con = new contrato();
            con.get(_gsql.getResultSet());
            _items.add(con);
        }
        return true;
    }

}
